﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SqlHelp;
using CommonCs;

namespace Users
{
    public class UserData
    {
        public String GetPassWordByUser(String uid)
        {
            String result = String.Empty;
            String sql = "select PassWord from Users where Uid =@Uid";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@Uid",uid)
            };
            DataTable dt = new DataTable();
            try
            {
                dt = SQLHELP.GetDataTableWithParm(sql, parm);
                if (dt == null || dt.Rows.Count != 1)
                {
                    result = "";
                }
                else
                {
                    result = dt.Rows[0][0].ToString();
                }
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "登录异常,msg:[" + Exceptions.loginSql + "]!");
                throw new Exception(Exceptions.loginSql);
            }
            return result;
        }

        public Model.Users GetUserByUid(String uid) 
        {
            Model.Users user = new Model.Users();
            String sql = "select * from users where uid =@uid";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@uid",uid)
            };
            DataTable dt = new DataTable();

            try
            {
                dt = SQLHELP.GetDataTableWithParm(sql, parm);
                if (dt == null || dt.Rows.Count != 1)
                {
                    user = null;
                }
                else
                {
                    user.Id = Convert.ToInt32(dt.Rows[0]["Id"].ToString());
                    user.Uid = dt.Rows[0]["Uid"].ToString();
                    user.PassWord = dt.Rows[0]["PassWord"].ToString();
                    user.Email = dt.Rows[0]["Email"].ToString();
                    user.QQ = dt.Rows[0]["QQ"].ToString();
                    user.RealName = dt.Rows[0]["RealName"].ToString();
                    user.Hobby = dt.Rows[0]["Hobby"].ToString();
                    user.Sinaweibo = dt.Rows[0]["Sinaweibo"].ToString();
                    user.IsAdmin = dt.Rows[0]["IsAdmin"].ToString();
                }
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "根据uid获取用户异常,msg:[" + Exceptions.getUserSql + "]!");
                throw new Exception(Exceptions.getUserSql);
            }

            return user;
        }

        public Model.Users GetUserById(String id)
        {
            Model.Users user = new Model.Users();
            String sql = "select * from users where id =@id";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@id",id)
            };
            DataTable dt = new DataTable();

            try
            {
                dt = SQLHELP.GetDataTableWithParm(sql, parm);
                if (dt == null || dt.Rows.Count != 1)
                {
                    user = null;
                }
                else
                {
                    user.Id = Convert.ToInt32(dt.Rows[0]["Id"].ToString());
                    user.Uid = dt.Rows[0]["Uid"].ToString();
                    user.PassWord = dt.Rows[0]["PassWord"].ToString();
                    user.Email = dt.Rows[0]["Email"].ToString();
                    user.QQ = dt.Rows[0]["QQ"].ToString();
                    user.RealName = dt.Rows[0]["RealName"].ToString();
                    user.Hobby = dt.Rows[0]["Hobby"].ToString();
                    user.Sinaweibo = dt.Rows[0]["Sinaweibo"].ToString();
                    user.IsAdmin = dt.Rows[0]["IsAdmin"].ToString();
                }
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "根据id获取用户异常,msg:[" + Exceptions.getUserSql + "]!");
                throw new Exception(Exceptions.getUserSql);
            }

            return user;
        }

        public int InsertUser(Model.Users user)
        {
            String sql = "insert into users values(@uid,@PassWord,@Email,@QQ,@RealName,@Hobby,@Sinaweibo,@IsAdmin)";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@Uid",user.Uid),
                new SqlParameter("@PassWord",user.PassWord),
                new SqlParameter("@Email",user.Email),
                new SqlParameter("@QQ",user.QQ),
                new SqlParameter("@RealName",user.RealName),
                new SqlParameter("@Hobby",user.IsAdmin),
                new SqlParameter("@Sinaweibo",user.IsAdmin),
                new SqlParameter("@IsAdmin","0")
            };
            int result = 0;
            try
            {
                result = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "添加用户[" + user.Uid + "]异常,msg:[" + Exceptions.insertUserSql + "]!");
                throw new Exception(Exceptions.insertUserSql);
            }
            return result;
        }

        public int DeleteUser(string id)
        {
            String sql = "delete users where id =@id";
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@id",id)
            };
            int result = 0;
            try
            {
                result = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "删除用户[" + id + "]异常,msg:[" + Exceptions.deleteuse + "]!");
                throw new Exception(Exceptions.deleteuse);
            }
            return result;
        }

        public DataTable GetUsers()
        {
            String sql = "select * from users";

            DataTable dt = null;

            try
            {
                dt = SQLHELP.GetDataTableNoParm(sql);
            }
            catch (Exception)
            {
                Common.WriteFile(CommonCs.Exceptions.Abnormal, "获取所有用户列表异常,msg:[" + Exceptions.getalluser + "]!");
                throw new Exception(CommonCs.Exceptions.getalluser);
            }

            return dt;
        }
    }
}
